using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace ReportMaintenanceUtility
{
	public class DefaultValues : GenericBase<DefaultValues>
	{
		#region PRIVATE MEMBER(S)
		private ValidValues _values = null;
		private DefaultValueType _seed = null;
		private Dictionary<string, DataSet> _dsLibrary = null;
		#endregion

		#region PUBLIC PROPERTIES

		public ValidValues ValidValues
		{
			get { return this._values; }
		}

		public bool HasData
		{
			get
			{
				return (this._values != null && this._values.HasData);
			}
		}

		#endregion

		#region CONSTRUCTOR(S)

		private DefaultValues(DefaultValues dv)
		{
			if (dv == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._seed = (DefaultValueType)dv.CreateSeedCopy();
				this._dsLibrary = dv._dsLibrary;
				base.CopyListeners(dv);
				this.Initialize();
			}
		}

		public DefaultValues(DefaultValueType dvt, Dictionary<string, DataSet> datasets)
		{
			if (dvt == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._seed = dvt;
				this._dsLibrary = datasets;
				this.Initialize();
			}
		}
		protected override void Initialize()
		{
			this._values = new ValidValues(this._seed.Items, this._dsLibrary, ValueType.DefaultValues);
			this._values.PropertyChanged += this.PropertyChangedListener;
		}
		#endregion

		#region INTERFACE IMPLEMENTATION(S)

		public override bool EqualTo(DefaultValues other)
		{
			if (other == null)
			{
				return false;
			}
			else
			{
				return CompareTo<ValidValues>.Compare(this._values, other._values);
			}
		}

		public override List<ChangeLog> SaveState()
		{
			List<object> items = new List<object>(this._seed.Items);
			List<ChangeLog> log = new List<ChangeLog>();
			
			if (this._isDirty)
			{
				#region VALID VALUES
				if (!CompareTo<ValidValues>.Compare(this._values, this._copy.ValidValues))
				{
					ChangeLog sub = null;
					if (this._values == null && this._copy.ValidValues != null)
					{
						//DefaultValue source was deleted... so remove from original object
						this._seed.Items = null;
						sub = new ChangeLog(ChangeType.ValidValuesDelete, this._copy._values, this._values);
					}
					else
					{
						if (this._values != null && this._copy.ValidValues == null) //DefaultValue.ValidValues is new...
						{
							sub = new ChangeLog(ChangeType.ValidValuesAdd, this._copy._values, this._values);
						}
						else //DefaultValue.ValidValues exists...
						{
							sub = new ChangeLog(ChangeType.ValidValuesChange, this._copy._values, this._values);
						}

						sub.RelatedChanges.AddRange(this._values.SaveState());
						this._seed.Items = this._values.GetSeedObject();
					}
					log.Add(sub);
				}
				#endregion
			}

			this.ClearState();
			return log;
		}

		public override void RestoreState()
		{
			if (this._copy != null)
			{
				this._values = this._copy.ValidValues;
				this._seed = this._copy._seed;
				this.CopyListeners(this._copy);
			}
			this.ClearState();
		}

		public override DefaultValues CreateDeepCopy()
		{
			if (this._copy != null)
			{
				return this._copy;
			}
			return new DefaultValues(this);
		}

		public DefaultValueType GetSeedObject()
		{
			return this._seed;
		}

		#endregion

		public override object CreateSeedCopy()
		{
			if (this._values != null)
			{
				return this._values.CreateSeedCopy();
			}
			else
			{
				DefaultValueType dvt = new DefaultValueType();
				ValuesType vt = new ValuesType();
				vt.Value = new string[] { string.Empty };
				dvt.Items = new object[] { vt };

				return dvt;
			}
		}

		public void ResetDataSetLibrary(Dictionary<string, DataSet> list)
		{
			this._dsLibrary = list;
			if (this._values != null)
			{
				this._values.ResetDataSetLibrary(list);
			}
		}
	}
}
